package com.ruiyi.careryun.web.ota;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruiyi.careryun.beans.VersionAndroid;
import com.ruiyi.careryun.beans.VersionMap;
import com.ruiyi.careryun.beans.common.ResultMsg;
import com.ruiyi.careryun.mapper.ota.VersionAndroidMapper;
import com.ruiyi.careryun.service.ota.VersionAndroidService;
import com.ruiyi.careryun.service.ota.VersionInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "安卓差分包")
@Controller
@RequestMapping("/ota/android")
public class VersionAndroidController {
    @Autowired
    private VersionAndroidService otaAndroidService;
    @Autowired
    private VersionAndroidMapper versionAndroidMapper;
    @Autowired
    private VersionInfoService versionInfoService;

    @ApiIgnore
    @RequestMapping("/toupload")
    public String index() {
        return "/ota/android/upload";
    }

    @ApiOperation(value = "版本更新", notes = "")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<Map<String, Object>> otaFileUpload(@RequestParam(value = "version", required = true) String newVersion, @RequestParam(value = "type", required = true) Integer type,
                                                        MultipartFile file, HttpServletRequest request) {
        try {
            return otaAndroidService.updatePackage(newVersion, file, type);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.error("版本更新失败");
        }
    }

    @RequestMapping("/checkupdate")
    @ResponseBody
    public ResultMsg<Map<String, Object>> checkUpdate(@RequestParam(value = "version", required = true) String androidVersion, @RequestParam(value = "type", required = true) Integer type) {
        Map<String, Object> returnInfo = new HashMap<>();
        returnInfo.put("needUpdate", false);
        returnInfo.put("patchUrl", "");
        returnInfo.put("completeUrl", "");
        // 获取旧版本号
        VersionAndroid lastVersionAndroid = versionAndroidMapper.getLastVersionInfoByType(type);
        if (androidVersion.equals(lastVersionAndroid.getVersion())) {
            return ResultMsg.error("已是最新版本");
        } else {
            String[] path = new String[] { "/fm", "/va", "/adr", "/m", "/cey", "/ad" };
            returnInfo.put("needUpdate", true);
            Integer diffCount = otaAndroidService.getVersionDiffCount(androidVersion, type);
            if (diffCount == 1) {
                returnInfo.put("patchUrl", path[type - 1] + "/" + androidVersion + "/" + "update.patch");
            } else {
                // 返回完整包路径
                returnInfo.put("completeUrl", path[type - 1] + "/" + lastVersionAndroid.getVersion() + "/" + "update.apk");
            }
            return ResultMsg.success(returnInfo);
        }
    }

    @RequestMapping("/checkupdaterom")
    @ResponseBody
    public ResultMsg<VersionMap> checkUpdateRom(@RequestParam(value = "version", required = true) String version, @RequestParam(value = "type", required = true) Integer type) {
        VersionMap versionMap = null;
        try {
            versionMap = versionInfoService.getUpdateInfo(version);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.error("服务器异常");
        }
        return ResultMsg.success(versionMap);
    }
}
